/*
 * @Author: lsmi
 * @Date: 2021-07-16 15:36:33
 * @LastEditors: lsmi
 * @LastEditTime: 2021-08-09 20:30:48
 * @FilePath: \v2_play_js\src\plugins\axios.js
 */
"use strict";

import Vue from "vue";
import axios from "axios";
import qs from "qs";

let config = {
  timeout: 60 * 1000,
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    let token = localStorage.getItem("token");
    config.headers["token"] = token;
    if (config.method == "post") {
      if (config.url.search("upload") != -1) {
        config.headers["Content-Type"] = "multipart/form-data";
      } else {
        config.data = qs.stringify(config.data);
        config.headers["Content-Type"] = "application/x-www-form-urlencoded";
      }
    }

    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);
// Add a response interceptor
_axios.interceptors.response.use(
  function (response) {
    let code = response.data.code;
    // const devURL = 'http://192.168.20.172'
    // const proURL = 'https://approve.api.laikapets.com'
    switch (code) {
      case 10000:
        break;
      case 405:
        break;
      case 403:
        break;
    }
    // Do something with response data
    return response;
  },
  function (error) {
    // Do something with response error
    return Promise.reject(error);
  }
);

Plugin.install = function (Vue, options) {
  Vue.axios = _axios;
  window.axios = _axios;
  Object.defineProperties(Vue.prototype, {
    axios: {
      get() {
        return _axios;
      },
    },
    $axios: {
      get() {
        return _axios;
      },
    },
  });
};

Vue.use(Plugin);
console.log(_axios, "_axios");
export default _axios;
